package com.renu.jdependencyanalyser.utils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

// todo
public class WebCrawler {
    //<a class="vuln" href="https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2022-42004">CVE-2022-42004</a>
    private static   String[] ua = {"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.87 Safari/537.36 OPR/37.0.2178.32",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36",
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586",
            "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko",
            "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)",
            "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)",
            "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0)",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 BIDUBrowser/8.3 Safari/537.36",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36 Core/1.47.277.400 QQBrowser/9.4.7658.400",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 UBrowser/5.6.12150.8 Safari/537.36",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36 SE 2.X MetaSr 1.0",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36 TheWorld 7",
            "Mozilla/5.0 (Windows NT 6.1; W…) Gecko/20100101 Firefox/60.0"};
    private static Random r = new Random();;

    public static  Map<String,String> crawlVulAndUsage(String group,String artificat,String version){
        String url = "http://mvnrepository.com/artifact/"+group+"/"+artificat+"/"+version+"";
        HashMap<String, String> resMap = new HashMap<>();
        int i = r.nextInt(14);
        Map<String,String> map = new HashMap<String,String>();
        map.put("waybillNo","DD1838768852");
        try {
            //爬取的目标网站，url记得换下。。。！！！  代理ip网站
            Document doc = Jsoup.connect(url)
                    .timeout(5000)
                    //.proxy(ip, port)
                    .data(map)
                    .ignoreContentType(true)
                    .userAgent(ua[i])
                    //.header("referer","http://xxxx.com/dayProxy.html")//这个来源记得换..
                    .post();
            //System.out.println("访问时间:"+(System.currentTimeMillis() -a) + "   访问结果: "+doc.text());

            // 选择包含 "Used By" 文本的元素
            Element usedByElement = doc.select("th:contains(Used By)").first();

            // 获取 "Used By" 元素相邻的下一个元素（即 <td> 标签）
            Element artifactsElement = usedByElement.nextElementSibling();

            // 获取 <td> 标签内的文本内容
            String artifacts = artifactsElement.text();
            resMap.put("Usage",artifacts);
            // 输出结果
            System.out.println("Used By: " + artifacts);
            // 使用 CSS 选择器找到具有 class="vuln" 的所有 <a> 元素
            Elements links = doc.select("a.vuln");

            // 遍历所有找到的 <a> 元素
            for (Element link : links) {
                // 获取 href 属性的值
                String href = link.attr("href");
                // 获取文本内容作为 name
                String name = link.text();
                resMap.put("Href",href);
                resMap.put("Vuln",name);
                // 输出 href 和 name
                System.out.println("Href: " + href);
                System.out.println("Vuln: " + name);
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("web error");
        }
        return resMap;
    }

    public static void main(String[] args) {
        {
            String url = "http://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind/2.13.3";

            Random r = new Random();
            String[] ua = {"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0",
                    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.87 Safari/537.36 OPR/37.0.2178.32",
                    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2",
                    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36",
                    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586",
                    "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko",
                    "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)",
                    "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)",
                    "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0)",
                    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 BIDUBrowser/8.3 Safari/537.36",
                    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36 Core/1.47.277.400 QQBrowser/9.4.7658.400",
                    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 UBrowser/5.6.12150.8 Safari/537.36",
                    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36 SE 2.X MetaSr 1.0",
                    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36 TheWorld 7",
                    "Mozilla/5.0 (Windows NT 6.1; W…) Gecko/20100101 Firefox/60.0"};
            int i = r.nextInt(14);
            Map<String,String> map = new HashMap<String,String>();
            map.put("waybillNo","DD1838768852");
            try {
                long a = System.currentTimeMillis();
                //爬取的目标网站，url记得换下。。。！！！  代理ip网站
                Document doc = Jsoup.connect(url)
                        .timeout(5000)
                        //.proxy(ip, port)
                        .data(map)
                        .ignoreContentType(true)
                        .userAgent(ua[i])
                        //.header("referer","http://xxxx.com/dayProxy.html")//这个来源记得换..
                        .post();
                //System.out.println("访问时间:"+(System.currentTimeMillis() -a) + "   访问结果: "+doc.text());
                // 选择包含 "Used By" 文本的元素
                Element usedByElement = doc.select("th:contains(Used By)").first();

                // 获取 "Used By" 元素相邻的下一个元素（即 <td> 标签）
                Element artifactsElement = usedByElement.nextElementSibling();

                // 获取 <td> 标签内的文本内容
                String artifacts = artifactsElement.text();

                // 输出结果
                System.out.println("Used By: " + artifacts);
                // 使用 CSS 选择器找到具有 class="vuln" 的所有 <a> 元素
                    Elements links = doc.select("a.vuln");

                    // 遍历所有找到的 <a> 元素
                    for (Element link : links) {
                        // 获取 href 属性的值
                        String href = link.attr("href");
                        // 获取文本内容作为 name
                        String name = link.text();

                        // 输出 href 和 name
                        System.out.println("Href: " + href);
                        System.out.println("Name: " + name);
                    }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        //try {
        //    // 使用Jsoup连接到指定URL并获取页面内容
        //    Document doc = Jsoup.connect(url).get();
        //
        //    // 使用 CSS 选择器找到具有 class="vuln" 的所有 <a> 元素
        //    Elements links = doc.select("a.vuln");
        //
        //    // 遍历所有找到的 <a> 元素
        //    for (Element link : links) {
        //        // 获取 href 属性的值
        //        String href = link.attr("href");
        //        // 获取文本内容作为 name
        //        String name = link.text();
        //
        //        // 输出 href 和 name
        //        System.out.println("Href: " + href);
        //        System.out.println("Name: " + name);
        //    }
        //} catch (IOException e) {
        //    e.printStackTrace();
        //}
    }
}
